﻿using System;
using System.Reflection.Emit;
using msilgc.cbuilder.CSyntaxTree;
using msilgc.cbuilder.MSILSyntaxTree;
using msilgc.CSyntaxTree;

namespace msilgc.cbuilder.CBuilder
{
    internal class NotTranslator : ITranslator
    {
        private readonly VariableFactory _varFactory;
        public NotTranslator(VariableFactory varFactory)
        {
            _varFactory = varFactory;
        }

        public ICStatement TranslateAndAdd(
            CMethodBody methodBody, CBasicBlock currentBlock, 
            BasicBlock block, Instruction instruction)
        {
            if(instruction.Inst != OpCodes.Not)
            {
                throw new ArgumentOutOfRangeException("instruction");
            }
            IVariable source = EvaluationStackHelper.PopVariableOffEvaluationStack(currentBlock);
            IVariable target = _varFactory.CreateNext(source.Type);
            NotStatement notStm = new NotStatement(source, target);
            currentBlock.Statements.Add(notStm);
            return notStm;
        }
    }
}
